import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

public class num17_14 {
    public int[] smallestK(int[] arr, int k) {
        int[] ret = new int[k];
        if(arr.length == 0 || k == 0){
            return ret;
        }
        Queue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for (int i = 0; i < arr.length; i++) {
            if(queue.size() < k){
                queue.offer(arr[i]);
            }else{
                int max = queue.peek();
                if(max > arr[i]){
                  queue.poll();
                  queue.offer(arr[i]);
                }
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i] = queue.poll();
        }
        return ret;
    }
}
